package com.darsystems.foghourz.client;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class StatusPanel extends VerticalPanel implements StatusDisplay {

	private final String startImage = "img/start.png";
	private final String commImage = "img/comm.png";
	private final String okImage = "img/ok.png";
	private final String loginPlease = "img/loginPlease.png";
	private final String loginAgain = "img/loginAgain.png";
	private final String problems = "img/problems.png";
	private final String googleSignIn = "img/googleSignIn.png";
	
	private final HorizontalPanel msgPanel = new HorizontalPanel();
	private final HorizontalPanel signOutLinksPanel = new HorizontalPanel();
	
	private final Image statusIcon = new Image(startImage);
	private final Label statusLabel = new Label();
	
	private Anchor signInLink = new Anchor();	
	//private Anchor signOutLink = new Anchor("Sign out of Google");	
	ArrayList<Authorizer> authorizers;
	ArrayList<Anchor> signOutLinks;
	
	int currentAuthorizer = 0;
	
	private LoginHandler allLoggedInHandler;
	
	public StatusPanel(String startMsg, ArrayList<Authorizer> authorizers, LoginHandler allLoggedIn)
	{
		this.authorizers = authorizers;
		allLoggedInHandler = allLoggedIn;
		
		statusIcon.setPixelSize(24, 24);
		msgPanel.setSpacing(5);
		msgPanel.add(statusIcon);
		msgPanel.add(statusLabel);
		msgPanel.add(signInLink);
		
		statusLabel.setText(startMsg);
		signInLink.setVisible(false);

		createSignoutLinks();

		signOutLinksPanel.setSpacing(5);
		add(signOutLinksPanel);
		add(msgPanel);
	}
	
	
	public void everythingOK(String message)
	{
		statusIcon.setUrl(okImage);
		statusLabel.setText(message);
	}
	
	public void setCommMessage(String message)
	{
		statusIcon.setUrl(commImage);
		statusLabel.setText(message);
	}
	
	public void loginMessage(boolean again)
	{
		statusIcon.setUrl(again ? loginAgain : loginPlease);
		statusLabel.setText("Login Please");
	}
	
	public void problem(String message)
	{
		statusIcon.setUrl(problems);
		statusLabel.setText(message);
	}

	public void getAuthorized()
	{
		if(currentAuthorizer >= authorizers.size())
		{
			allLoggedInHandler.loginSucceeded();
			return;
		}
		
		final Authorizer authorizer = authorizers.get(currentAuthorizer);
		final int authIndex = currentAuthorizer;
		
		authorizer.checkLoginStatus(new AuthorizeCallback(){
			public void onFailure(Throwable caught) {
				problem(caught.getMessage());
				
			}
			public void onLoggedIn(String logoutUrl) {
				signOutLinks.get(authIndex).setHref(logoutUrl);
	        	signOutLinks.get(authIndex).setVisible(true);
	        	setCurrentAuthorizerIndex(authIndex + 1);
	        	getAuthorized();
			}
			public void onNotLoggedIn(String loginUrl) {
				if(authorizer.getAutoRedirect())
				{
					Window.Location.replace(loginUrl);
				}
				else
				{
					allowLogin(authorizer.getName(), loginUrl);
				}
			}
			
		});
	}
	
	private void setCurrentAuthorizerIndex(int index)
	{
		currentAuthorizer = index;
	}
	private void allowLogin(String authorizerName, String loginUrl)
	{
		statusLabel.setText("");
		statusIcon.setUrl(googleSignIn);
		
		signInLink.setText("Sign in to " + authorizerName);
		signInLink.setHref(loginUrl);
		signInLink.setVisible(true);
	}
	
	private void createSignoutLinks()
	{
		signOutLinks = new ArrayList<Anchor>();
		for(int i = 0; i < authorizers.size(); ++i)
		{
			Anchor signOutLink = new Anchor("Sign out of " + authorizers.get(i).getName());
			signOutLink.setVisible(false);
			signOutLink.setStylePrimaryName("signOutLink");	
			signOutLinks.add(signOutLink);
			signOutLinksPanel.add(signOutLink);
		}
	}
	
}
